home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 1 / CU Amiga Magazine CD-ROM Special Edition (1995)(EMAP Images)(GB)[Issue 1995-11].iso / Aminet / comm / tcp / AmiTCPsdk_40.lha / AmiTCP-4.0 / src / netlib / _read.c < prev    next >
C/C++ Source or Header  |  1994-09-29  |  2KB  |  101 lines

  1. RCS_ID_C="$Id: _read.c,v 4.1 1994/09/29 23:09:02 jraja Exp $";
  2. /*
  3.  *      _read.c - read() for both files and sockets (SAS/C)
  4.  *
  5.  *      Copyright © 1994 AmiTCP/IP Group, 
  6.  *                       Network Solutions Development Inc.
  7.  *                       All rights reserved.
  8.  */
  9.  
  10. #include <ios1.h>
  11. #include <stdlib.h>
  12. #include <dos.h>
  13. #include <string.h>
  14. #include <errno.h>
  15. #include <dos/dos.h>
  16. #include <proto/dos.h>
  17.  
  18. #include <bsdsocket.h>
  19.  
  20. extern int __io2errno(long);
  21.  
  22. int
  23. __read(int fd, void *buffer, unsigned int length)
  24. {
  25.   struct UFB *ufb;
  26.   int         count;
  27.   char        ch, *ptr, *nextptr, *endptr;
  28.  
  29.   /*
  30.    * Check for the break signals
  31.    */
  32.   __chkabort();
  33.   /*
  34.    * find the ufb *
  35.    */
  36.   if ((ufb = __chkufb(fd)) == NULL) {
  37.     errno = EINVAL;
  38.     return -1;
  39.   }
  40.   /*
  41.    * Check if read is allowed
  42.    */
  43.   if (!(ufb->ufbflg & UFB_RA)) {
  44.     _OSERR = ERROR_READ_PROTECTED;
  45.     errno = EIO;
  46.     return -1;
  47.   }
  48.  
  49.   /*
  50.    * Do the Actual read
  51.    */
  52.   _OSERR = 0;
  53.   if (ufb->ufbflg & UFB_SOCK) {
  54.     if ((count = recv(fd, (UBYTE *)buffer, length, 0)) < 0) {
  55.       return -1;
  56.     }
  57.   }
  58.   else {
  59.     if ((count = Read(ufb->ufbfh, buffer, length)) == -1) {
  60.       errno = __io2errno(_OSERR = IoErr());
  61.       return -1;
  62.     }
  63.   }
  64.   /*
  65.    * Check if translation is not needed
  66.    */
  67.   if (count == 0 || !(ufb->ufbflg & UFB_XLAT))
  68.     return count;
  69.   
  70.   endptr = (char *)buffer + count;    /* first point NOT in buffer */
  71.   
  72.   if (endptr[-1] == 0x0D)    /* checks last char */
  73. #if 0
  74.     Read(ufb->ufbfh, buffer, 1); /* overwrites first read byte ! */
  75.   /*            BUG  ^^^^^^ */
  76. #else
  77.     count--, endptr--;
  78. #endif
  79.  
  80.   /*
  81.    * Remove 0x0D's (CR) (This doesn't remove a CR at the end of the buffer).
  82.    */
  83.   if ((ptr = memchr(buffer, 0x0D, count)) != NULL) {
  84.     nextptr = ptr + 1;
  85.     while (nextptr < endptr) {
  86.       if ((ch = *nextptr) != 0x0D)
  87.     *ptr++ = ch;
  88.       nextptr++;
  89.     }
  90.     count = ptr - (char *)buffer;
  91.   }
  92.  
  93.   /*
  94.    * Test for CTRL-Z (end of file)
  95.    */
  96.   if ((ptr = memchr(buffer, 0x1A, count)) == NULL)
  97.     return count;
  98.  
  99.   return ptr - (char *)buffer;
  100. }
  101.